<?php
namespace common\modules\user\frontend\models;

use common\modules\user\models\User;
use Yii;
use yii\base\Model;


/**
 * Signup form
 */
class SignupForm extends Model
{

    public $username;

    public $email;

    public $password;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                'username',
                'filter',
                'filter' => 'trim'
            ],
            [
                'username',
                'required'
            ],
            [
                'username',
                'unique',
                'targetClass' => '\common\models\User',
                'message' => Yii::t('frontend', 'This username has already been taken.')
            ],
            [
                'username',
                'string',
                'min' => 2,
                'max' => 255
            ],
            
            [
                'email',
                'filter',
                'filter' => 'trim'
            ],
            [
                'email',
                'required'
            ],
            [
                'email',
                'email'
            ],
            [
                'email',
                'unique',
                'targetClass' => '\common\models\User',
                'message' => Yii::t('frontend', 'This email address has already been taken.')
            ],
            
            [
                'password',
                'required'
            ],
            [
                'password',
                'string',
                'min' => 6
            ]
        ];
    }

    public function attributeLabels()
    {
        return [
            'username' => Yii::t('frontend', 'Username'),
            'email' => Yii::t('frontend', 'E-mail'),
            'password' => Yii::t('frontend', 'Password')
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->username = $this->username;
            $user->email = $this->email;
            $user->setPassword($this->password);
            $user->save();
            $user->afterSignup();
            return $user;
        }
        
        return null;
    }
}
